<template>
  <header class="searchs">
    <div class="title">
      <section class="m-search-book">
        <router-link to="/" class="left">
          <span class="ui-leftaw-2"></span>
        </router-link>
        <div class="search-input">
          <div class="sech">
            <div class="inner">
              <button @click="getkeyword"></button>
              <input
                placeholder="唐诗里的烟火人间"
                @keydown.enter="getkeyword"
                @input="getlist"
              />
            </div>
            <router-link to="/home" class="home">首页</router-link>
          </div>
        </div>
        <div class="timelist" v-show="shows">
          <ul id="J_SearchOps" style="">
            <li class="hasimg" v-for="(item, x) in serchlist" v-if="x <= 0">
              <router-link :to="`/cs?id=${item.id}&title=${item.text}`"
                ><img :src="item.cover" />
                <h3>{{ item.text }}</h3>
                <p>{{ item.author }}</p>
                <p class="desc">{{ item.d }}</p></router-link
              >
            </li>
            <li class="children" v-for="(item, x) in serchlist" v-if="x > 0">
              <router-link :to="`/cs?id=${item.id}&title=${item.text}`"
                ><em></em>{{ item.text }}</router-link
              >
            </li>
          </ul>
        </div>
      </section>
      <div class="hot">
        <div class="m-hotkey">
          <div class="h2">大家都在搜</div>
          <ul>
            <li v-for="(item, x) in hot_list">
              <router-link :to="`/cs?id=${item.id}&title=${item.name}`"
                ><em class=""></em>{{ item.name }}</router-link
              >
            </li>
          </ul>
        </div>
      </div>
      <div class="hot">
        <div class="m-hotkey">
          <div class="h2">最近搜索</div>
          <ul style="display: none">
            <li><router-link to=""><em class=""></em>一剑独尊</router-link></li>
            <li><router-link to=""><em class=""></em>一剑独尊</router-link></li>
            <li><router-link to=""><em class=""></em>一剑独尊</router-link></li>
            <li><router-link to=""><em class=""></em>一剑独尊</router-link></li>
          </ul>
        </div>
      </div>
    </div>
  </header>
</template>
<script>
import { hotFunc, serchlistFunc } from "@/request/index";
export default {
  data() {
    return {
      hot_list: [],
      serchlist: [],
      shows: false,
    };
  },
  methods: {
    async csfe() {
      await hotFunc().then((res) => {
        console.log(res);
        let hotlist = res.list;
        this.hot_list = hotlist.map((item) => {
          console.log(item);
          return item;
        });
      });
    },
    getkeyword() {
      let kework = document.querySelector("input").value;
      console.log(kework);
      if (kework !== "") {
        console.log(kework);
        this.$router.push(`/searchlist?keword=${kework}`);
      } else {
        window.alert("请输入内容后搜索");
      }
      // serchFunc(kework).then((res) => {
      //   console.log(res)
      //   let bookdata=res.data.book
      // });
    },
    getlist() {
      let kework = document.querySelector("input").value;
      // console.log()
      if (kework !== "" || kework !== undefined || kework !== null) {
        serchlistFunc(document.querySelector("input").value).then((res) => {
          console.log();
          let boolist = res.words.w;
          this.serchlist = boolist;
          console.log(this.serchlist);
          if (this.serchlist !== undefined) {
            this.shows = true;
          } else if (this.serchlist === undefined) {
            this.shows = false;
          }
        }).catch(err => {
          console.log("请求失败...");
        });
      }
    },
    moveos(e) {
      if (e.deltaY > -430) {
        // console.log("cg")
        this.shows = false;
      }
    },
  },
  created() {
    this.csfe();
  },
};
</script>
<style lang="scss">
@import url(../static/css/reset.css);
.searchs {
  width: 100%;
  .title {
    position: relative;
    height: 44px;
    line-height: 44px;
    min-width: 256px;
    background-color: black;
    a.left {
      float: left;
      font-size: 14px;
      position: relative;
      .ui-leftaw-2 {
        display: inline-block;
        margin-bottom: 3px;
        margin-left: 15px;
        margin-right: 15px;
        vertical-align: middle;
        width: 10px;
        height: 18px;
        background: url(../static/imgs/arrow-left.png);
        background-size: 100% 100%;
      }
    }
    .search-input {
      margin-left: 40px;
      padding: 8px 15px 8px 0;
      box-sizing: border-box;
      min-width: 256px;
      .inner {
        position: relative;
        width: 85%;
        button {
          width: 51px;
          height: 28px;
          border: none;
          vertical-align: middle;
          background: url(../static/imgs/search.png);
          background-size: cover;
          background-repeat: no-repeat;
          position: absolute;
          top: 0;
          right: 0;
        }
        input {
          border: none;
          height: 28px;
          line-height: 28px;
          font-size: 12px;
          vertical-align: middle;
          float: left;
          box-sizing: border-box;
          color: #6c6358;
          background: #fff;
          border-radius: 14px;
          width: 100%;
          padding: 0 51px 0 15px;
        }
      }
      a.home {
        float: left;
        width: 15%;
        height: 28px;
        line-height: 28px;
        color: #fff;
        font-size: 15px;
        padding: 0;
        text-align: right;
      }
    }
  }
  .hot {
    width: 100%;
    clear: both;
    .m-hotkey {
      margin-block-end: -2em;
      .h2 {
        font-size: 14px;
        text-indent: 2px;
        color: #aaa;
        line-height: 38px;
        background: #f5f5f5;
        color: #807a73;
        padding: 0 15px;
        margin-block-start: 2em;
        margin-block-end: 0;
      }
      ul {
        padding: 15px;
        font-size: 0;
        margin-inline-start: 5%;
        display: block;
        li{
          float: left;
          a {
          background: #fff;
          font-size: 14px;
          padding: 0 10px;
          line-height: 28px;
          height: 28px;
          margin-right: 15px;
          margin-bottom: 15px;
          display: inline-block;
          color: #aba59a;
          border: 1px solid #c8c8c8;
          border-radius: 4px;
          position: relative;
        }
        }
      }
    }
  }
  .timelist {
    ul {
      overflow-y: scroll;
      top: 60px;
      left: 0;
      right: 0;
      clear: both;
      background-color: #fff;
      padding-left: 15px;
      height: 450px;
      li {
        &.hasimg {
          height: 70px;
          padding: 10px 50px 10px 0;
          border-bottom: #ededed solid 1px;
          img {
            float: left;
            margin: 0 10px 0 0;
            width: 53px;
            height: 70px;
          }
          h3 {
            font-size: 16px;
            line-height: 18px;
            height: 18px;
            overflow: hidden;
            word-break: break-all;
            font-weight: normal;
            color: #6c6358;
            text-overflow: ellipsis;
            white-space: nowrap;
            padding-bottom: 2px;
          }
          p {
            font-size: 12px;
            margin-top: 4px;
            color: #aba59a;
            line-height: 16px;
            height: 16px;
            overflow: hidden;
            &.desc {
              font-size: 13px;
              color: #807a73;
              height: 32px;
              line-height: 16px;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
        &.children {
          height: 50px;
          line-height: 50px;
          border-bottom: #ededed solid 1px;
          font-size: 16px;
          color: #6c6358;
          padding-right: 50px;
          a {
            display: block;
            height: 100%;
            color: #777;
            position: relative;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          em {
            background: url(../static/imgs/search-list.png) center center
              no-repeat;
            width: 14px;
            height: 14px;
            background-size: contain;
            float: left;
            margin: 17px 5px 0 0;
          }
        }
      }
    }
  }
}
</style>